package cn.edu.qzuie.artist.module.td.service.publishactivity;

import cn.edu.qzuie.artist.module.td.controller.admin.participateactivitiy.vo.ParticipateActivitiyPageReqVO;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.edu.qzuie.artist.module.td.controller.admin.publishactivity.vo.*;
import cn.edu.qzuie.artist.module.td.dal.dataobject.publishactivity.PublishActivityDO;
import cn.edu.qzuie.artist.framework.common.pojo.PageResult;
import cn.edu.qzuie.artist.framework.common.pojo.PageParam;
import cn.edu.qzuie.artist.framework.common.util.object.BeanUtils;

import cn.edu.qzuie.artist.module.td.dal.mysql.publishactivity.PublishActivityMapper;

import static cn.edu.qzuie.artist.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.edu.qzuie.artist.module.td.enums.ErrorCodeConstants.*;

/**
 *  发布活动 Service 实现类
 *
 * @author 泉信管理员
 */
@Service
@Validated
public class PublishActivityServiceImpl implements PublishActivityService {

    @Resource
    private PublishActivityMapper publishActivityMapper;

    @Override
    public Long createPublishActivity(PublishActivitySaveReqVO createReqVO) {
        // 插入
        PublishActivityDO publishActivity = BeanUtils.toBean(createReqVO, PublishActivityDO.class);
        publishActivityMapper.insert(publishActivity);
        // 返回
        return publishActivity.getId();
    }

    @Override
    public void updatePublishActivity(PublishActivitySaveReqVO updateReqVO) {
        // 校验存在
        validatePublishActivityExists(updateReqVO.getId());
        // 更新
        PublishActivityDO updateObj = BeanUtils.toBean(updateReqVO, PublishActivityDO.class);
        publishActivityMapper.updateById(updateObj);
    }

    @Override
    public void deletePublishActivity(Long id) {
        // 校验存在
        validatePublishActivityExists(id);
        // 删除
        publishActivityMapper.deleteById(id);
    }

    private void validatePublishActivityExists(Long id) {
        if (publishActivityMapper.selectById(id) == null) {
            throw exception(PUBLISH_ACTIVITY_NOT_EXISTS);
        }
    }

    @Override
    public PublishActivityDO getPublishActivity(Long id) {
        return publishActivityMapper.selectById(id);
    }

    @Override
    public PageResult<PublishActivityDO> getPublishActivityPage(PublishActivityPageReqVO pageReqVO) {
        return publishActivityMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<PublishActivityPagesRespVO> getPublishActivityPageWithName(PublishActivityPageReqVO pageReqVO) {
        PublishActivityPageReqVO reqVO = new PublishActivityPageReqVO();
        BeanUtils.copyProperties(pageReqVO,reqVO);
        return publishActivityMapper.selectPageWithName(pageReqVO,reqVO);

    }


}